﻿using System.Web.Mvc;
using Century.Common.Business.BOL.Common;
using Century.EnterpriseLibrary.Configuration;
using Century.Models.Common;
using Telerik.Web.Mvc;
using System.Linq;
using Century.Web.Security.Attributes;
using Century.Web.UI.MVC;

namespace Century.Admin.Controllers
{
    [Authorization(Group = "", Name = "ConfManager")]
    public class ConfManagerController : CenturyAdminBaseController
    {
        SystemConfigurationBSO db = new SystemConfigurationBSO();
        SystemConfigurationPropertyBSO probDb = new SystemConfigurationPropertyBSO();
        public ViewResult Index()
        {

            return View();

        }


        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditSystemConfiguration(int id)
        {
            SystemConfiguration systemConfiguration = db.Find(p => p.Id == id);
            TryUpdateModel(systemConfiguration);
            db.Update(systemConfiguration);
            var d = new GridModel
            {
                Data = db.GetAll().Select(o => new
                {
                    o.Id,
                    o.Owner,
                    o.ConfigKey,
                    o.ConfigValue,
                    o.ConfigCode,
                    o.Description,
                    o.IsEncrypt,
                    o.ValueType,
                    o.ListSeparator
                }).OrderBy(c=>c.Owner)
            };
            return View(d);
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult InsertSystemConfiguration()
        {
            //Create a new instance of the EditableProduct class.
            SystemConfiguration systemConfiguration = new SystemConfiguration { Id = db.GetMax() + 1 };
            //Perform model binding (fill the product properties and validate it).
            if (TryUpdateModel(systemConfiguration))
            {
                //The model is valid - insert the product.
                db.Insert(systemConfiguration);
            }
            //Rebind the grid
            var d = new GridModel
            {
                Data = db.GetAll().Select(o => new
                {
                    o.Id,
                    o.Owner,
                    o.ConfigKey,
                    o.ConfigValue,
                    o.ConfigCode,
                    o.Description,
                    o.IsEncrypt,
                    o.ValueType,
                    o.ListSeparator
                }).OrderBy(c => c.Owner)
            };
            return View(d);
        }

        [GridAction]
        public ActionResult GetSystemConfigurationList()
        {
            var d = new GridModel
            {
                Data = db.GetAll().Select(o => new
                {
                    o.Id,
                    o.Owner,
                    o.ConfigKey,
                    o.ConfigValue,
                    o.ConfigCode,
                    o.Description,
                    o.IsEncrypt,
                    o.ValueType,
                    o.ListSeparator
                }).OrderBy(c => c.Owner)
            };
            return View(d);
        }

        [GridAction]
        public ActionResult DetailsForConfig(int configId)
        {
            var confDetails = db.GetSystemConfigurationProperties(configId).Select(c =>
                new
                {
                    c.PropertyKey,
                    c.PropertyValue
                });
            return View(new GridModel(confDetails));
        }

        
        [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
        [GridAction]
        public ActionResult EditDetailsForConfig(int configId, string propertyKey)
        {
            SystemConfigurationProperty systemConfigurationProperty = probDb.Find(c => c.SystemConfigurationId == configId && c.PropertyKey == propertyKey);
            TryUpdateModel(systemConfigurationProperty);
            systemConfigurationProperty.SystemConfigurationId = configId;
            probDb.Update(systemConfigurationProperty);
            var confDetails = db.GetSystemConfigurationProperties(configId).Select(c =>
                new
                {
                    c.PropertyKey,
                    c.PropertyValue
                });
            return View(new GridModel(confDetails));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [GridAction]
        public ActionResult Reload()
        {
            ConfManager.Instance.ReLoad();
            var d = new GridModel
            {
                Data = db.GetAll().Select(o => new
                {
                    o.Id,
                    o.Owner,
                    o.ConfigKey,
                    o.ConfigValue,
                    o.ConfigCode,
                    o.Description,
                    o.IsEncrypt,
                    o.ValueType,
                    o.ListSeparator
                }).OrderBy(c => c.Owner)
            };
           
            return View(d);
        }
    }
}